﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using LibHuman;
using System.Data;
namespace Human
{
	public class frmThangnam : System.Windows.Forms.Form
	{
		AccessData acc = new AccessData();
        Language lan = new Language();
        DataSet ds = new DataSet();
        bool m_admin = false;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtThang;
		private System.Windows.Forms.TextBox txtNam;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button cmdOk;
		private System.Windows.Forms.Button cmdCancel;
		string s_mmyy="",s_userid,user="";
        DateTime d1, d2;
        private GroupBox gCheck_new;
        private RadioButton rdbKiemtra;
        private RadioButton rdbNew;
        private Label lbl;

		private System.ComponentModel.Container components = null;

		public frmThangnam(string s_user,bool admin)
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
			s_userid=s_user;
            m_admin = admin;
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmThangnam));
            this.label1 = new System.Windows.Forms.Label();
            this.txtThang = new System.Windows.Forms.TextBox();
            this.txtNam = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.cmdOk = new System.Windows.Forms.Button();
            this.cmdCancel = new System.Windows.Forms.Button();
            this.gCheck_new = new System.Windows.Forms.GroupBox();
            this.rdbNew = new System.Windows.Forms.RadioButton();
            this.rdbKiemtra = new System.Windows.Forms.RadioButton();
            this.lbl = new System.Windows.Forms.Label();
            this.gCheck_new.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.label1.Location = new System.Drawing.Point(19, 21);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(48, 16);
            this.label1.TabIndex = 0;
            this.label1.Text = "Tháng: ";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtThang
            // 
            this.txtThang.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtThang.ForeColor = System.Drawing.Color.Blue;
            this.txtThang.Location = new System.Drawing.Point(63, 19);
            this.txtThang.Name = "txtThang";
            this.txtThang.Size = new System.Drawing.Size(48, 22);
            this.txtThang.TabIndex = 1;
            this.txtThang.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtThang.Validated += new System.EventHandler(this.txtThang_Validated);
            this.txtThang.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtThang_KeyDown);
            // 
            // txtNam
            // 
            this.txtNam.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtNam.ForeColor = System.Drawing.Color.Blue;
            this.txtNam.Location = new System.Drawing.Point(160, 19);
            this.txtNam.Name = "txtNam";
            this.txtNam.Size = new System.Drawing.Size(48, 22);
            this.txtNam.TabIndex = 3;
            this.txtNam.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtNam.Validated += new System.EventHandler(this.txtNam_Validated);
            this.txtNam.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtNam_KeyDown);
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.label2.Location = new System.Drawing.Point(114, 22);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(40, 16);
            this.label2.TabIndex = 2;
            this.label2.Text = "Năm:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // cmdOk
            // 
            this.cmdOk.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cmdOk.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.cmdOk.Image = ((System.Drawing.Image)(resources.GetObject("cmdOk.Image")));
            this.cmdOk.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.cmdOk.Location = new System.Drawing.Point(29, 117);
            this.cmdOk.Name = "cmdOk";
            this.cmdOk.Size = new System.Drawing.Size(70, 28);
            this.cmdOk.TabIndex = 4;
            this.cmdOk.Text = "  Đồn&g ý";
            this.cmdOk.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.cmdOk.Click += new System.EventHandler(this.cmdOk_Click);
            // 
            // cmdCancel
            // 
            this.cmdCancel.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cmdCancel.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.cmdCancel.Image = ((System.Drawing.Image)(resources.GetObject("cmdCancel.Image")));
            this.cmdCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.cmdCancel.Location = new System.Drawing.Point(105, 117);
            this.cmdCancel.Name = "cmdCancel";
            this.cmdCancel.Size = new System.Drawing.Size(70, 28);
            this.cmdCancel.TabIndex = 5;
            this.cmdCancel.Text = "&Bỏ qua";
            this.cmdCancel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.cmdCancel.Click += new System.EventHandler(this.button2_Click);
            // 
            // gCheck_new
            // 
            this.gCheck_new.Controls.Add(this.rdbNew);
            this.gCheck_new.Controls.Add(this.rdbKiemtra);
            this.gCheck_new.Location = new System.Drawing.Point(24, 44);
            this.gCheck_new.Name = "gCheck_new";
            this.gCheck_new.Size = new System.Drawing.Size(183, 47);
            this.gCheck_new.TabIndex = 6;
            this.gCheck_new.TabStop = false;
            this.gCheck_new.Tag = "1";
            // 
            // rdbNew
            // 
            this.rdbNew.AutoSize = true;
            this.rdbNew.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.rdbNew.Location = new System.Drawing.Point(101, 18);
            this.rdbNew.Name = "rdbNew";
            this.rdbNew.Size = new System.Drawing.Size(63, 17);
            this.rdbNew.TabIndex = 1;
            this.rdbNew.Text = "Tạo mới";
            this.rdbNew.UseVisualStyleBackColor = true;
            this.rdbNew.CheckedChanged += new System.EventHandler(this.rdbNew_CheckedChanged);
            // 
            // rdbKiemtra
            // 
            this.rdbKiemtra.AutoSize = true;
            this.rdbKiemtra.Checked = true;
            this.rdbKiemtra.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.rdbKiemtra.Location = new System.Drawing.Point(12, 18);
            this.rdbKiemtra.Name = "rdbKiemtra";
            this.rdbKiemtra.Size = new System.Drawing.Size(63, 17);
            this.rdbKiemtra.TabIndex = 0;
            this.rdbKiemtra.TabStop = true;
            this.rdbKiemtra.Text = "Kiểm tra";
            this.rdbKiemtra.UseVisualStyleBackColor = true;
            this.rdbKiemtra.CheckedChanged += new System.EventHandler(this.rdbKiemtra_CheckedChanged);
            // 
            // lbl
            // 
            this.lbl.Location = new System.Drawing.Point(21, 96);
            this.lbl.Name = "lbl";
            this.lbl.Size = new System.Drawing.Size(185, 18);
            this.lbl.TabIndex = 7;
            this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // frmThangnam
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(232, 165);
            this.Controls.Add(this.lbl);
            this.Controls.Add(this.gCheck_new);
            this.Controls.Add(this.cmdCancel);
            this.Controls.Add(this.cmdOk);
            this.Controls.Add(this.txtNam);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txtThang);
            this.Controls.Add(this.label1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmThangnam";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Tạo dữ liệu chấm công";
            this.Load += new System.EventHandler(this.frmThangnam_Load);
            this.gCheck_new.ResumeLayout(false);
            this.gCheck_new.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmThangnam_Load(object sender, System.EventArgs e)
		{
			txtThang.Text=DateTime.Now.Month.ToString().PadLeft(2,'0');
			txtNam.Text=DateTime.Now.Year.ToString();
            d1 = new DateTime(int.Parse(txtNam.Text.Trim()), int.Parse(txtThang.Text.Trim()), 1);
            d2 = d1.AddMonths(1);
            user = acc.user;
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.Close();
        }		

		private void cmdOk_Click(object sender, System.EventArgs e)
		{
			s_mmyy=txtThang.Text+txtNam.Text.Substring(2);            
            switch (gCheck_new.Tag.ToString())
            {
                case "2":// neu chon rdbNew
                if (acc.bMmyy(s_mmyy))
                {
                    MessageBox.Show(lan.Change_language_MessageText("Dữ liệu tháng") + txtThang.Text + lan.Change_language_MessageText("năm")+ txtNam.Text + lan.Change_language_MessageText("đã có!"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                acc.taotable_hesonv(s_mmyy, s_userid);
                acc.taotable_thchamcong(s_mmyy, s_userid);
                if (acc.bVantay(s_mmyy))
                {
                    acc.taotable_chamcongvao(s_mmyy);
                    acc.taotable_chamcongra(s_mmyy);
                    acc.taotable_chamcongnghi(s_mmyy);
                }
                else
                {
                    acc.taotable(s_mmyy, s_userid);
                    acc.taotable_thuongtruc(s_mmyy);
                    acc.taotable_ngoaigio(s_mmyy);
                    acc.taotable_nvngoaigio(s_mmyy);
                    if (acc.cc_tructc(s_mmyy).Trim()=="1")
                    {
                        acc.taotable_tangcuong(s_mmyy);
                        acc.taotable_tructc(s_mmyy);
                    }
                    if (acc.cc_thuongquy(s_mmyy).Trim() == "1")
                    {
                        acc.taotable_nvthuongquy(s_mmyy);
                        acc.taotable_thuongquy(s_mmyy);
                    }                
                }
                if (MessageBox.Show(lan.Change_language_MessageText("Bạn có muốn tạo dữ liệu chấm công tháng?") + txtThang.Text +lan.Change_language_MessageText("năm")+ txtNam.Text +lan.Change_language_MessageText("không?"), AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    acc.taodulieu_thchamcong(s_mmyy);
                    acc.taodulieu_hesonv(s_mmyy, txtThang.Text.Trim(), txtNam.Text.Trim());
                    if (!acc.bVantay(s_mmyy))
                    {
                        acc.taodulieu_chamcong(s_mmyy,txtNam.Text.Trim());
                        acc.taodulieu_chamcong_ng(s_mmyy);
                        acc.taodulieu_chamcong_thuongtruc(s_mmyy,txtNam.Text.Trim());
                        if (acc.cc_tructc(s_mmyy).Trim() == "1")
                        {
                            acc.taodulieu_chamcong_tructc(s_mmyy);
                        }
                        if (acc.cc_thuongquy(s_mmyy).Trim() == "1")
                        {
                            acc.taodulieu_chamcong_thuongquy(s_mmyy); 
                        }
                    }
                }
                //
                if (!acc.byyyy(txtNam.Text))
                {
                    acc.taotable_phep(txtNam.Text, s_userid);
                    acc.taodulieu_phepnam(txtNam.Text,s_mmyy);
                    upd_phepcon(txtNam.Text.Trim());
                }
                if (int.Parse(txtThang.Text.Trim()) == (acc.khoadl_phep(s_mmyy)))
                {
                    ktthang_khoaphep(s_mmyy, txtThang.Text.Trim(), txtNam.Text.Trim());
                }
                acc.upd_table(s_mmyy, s_userid);
                MessageBox.Show(lan.Change_language_MessageText("Đã tạo xong!"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); 
                break;
                case "1":// Neu chon rdbKiemtra
                    if (!acc.bMmyy(s_mmyy))
                    {
                        if (MessageBox.Show(lan.Change_language_MessageText("Dữ liệu tháng") + txtThang.Text + lan.Change_language_MessageText("năm") + txtNam.Text + lan.Change_language_MessageText("chưa có!\n Bạn có muốn tạo mới không?"), this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            if (!acc.kt_heso(s_mmyy))
                            {
                                tao_dlheso(s_mmyy, acc.Mmyy_truoc(s_mmyy));
                            }
                            acc.taotable_hesonv(s_mmyy, s_userid);
                            acc.taotable_thchamcong(s_mmyy, s_userid);
                            if (acc.bVantay(s_mmyy))
                            {
                                acc.taotable_chamcongvao(s_mmyy);
                                acc.taotable_chamcongra(s_mmyy);
                                acc.taotable_chamcongnghi(s_mmyy);
                            }
                            else
                            {
                                acc.taotable(s_mmyy, s_userid);
                                acc.taotable_thuongtruc(s_mmyy);
                                acc.taotable_ngoaigio(s_mmyy);
                                acc.taotable_nvngoaigio(s_mmyy);
                                if (acc.cc_tructc(s_mmyy).Trim() == "1")
                                {
                                    acc.taotable_tangcuong(s_mmyy);
                                    acc.taotable_tructc(s_mmyy);
                                }
                                if (acc.cc_thuongquy(s_mmyy).Trim() == "1")
                                {
                                    acc.taotable_nvthuongquy(s_mmyy);
                                    acc.taotable_thuongquy(s_mmyy);
                                }
                            }
                            if (MessageBox.Show(lan.Change_language_MessageText("Bạn có muốn tạo dữ liệu chấm công tháng?") + txtThang.Text +lan.Change_language_MessageText("năm") + txtNam.Text +lan.Change_language_MessageText("không?"), AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                            {
                                acc.taodulieu_thchamcong(s_mmyy);
                                acc.taodulieu_hesonv(s_mmyy, txtThang.Text.Trim(), txtNam.Text.Trim());
                                if (!acc.bVantay(s_mmyy))
                                {
                                    acc.taodulieu_chamcong(s_mmyy,txtNam.Text.Trim());
                                    acc.taodulieu_chamcong_ng(s_mmyy);
                                    acc.taodulieu_chamcong_thuongtruc(s_mmyy,txtNam.Text.Trim());
                                    if (acc.cc_tructc(s_mmyy).Trim() == "1")
                                    {
                                        acc.taodulieu_chamcong_tructc(s_mmyy);
                                    }
                                    if (acc.cc_thuongquy(s_mmyy).Trim() == "1")
                                    {
                                        acc.taodulieu_chamcong_thuongquy(s_mmyy);
                                    }
                                }
                            }
                            if (!acc.byyyy(txtNam.Text))
                            {
                                acc.taotable_phep(txtNam.Text, s_userid);
                                acc.taodulieu_phepnam(txtNam.Text,s_mmyy);
                                upd_phepcon(txtNam.Text.Trim());
                            }
                            if (int.Parse(txtThang.Text.Trim()) == (acc.khoadl_phep(s_mmyy)))
                            {
                                ktthang_khoaphep(s_mmyy, txtThang.Text.Trim(), txtNam.Text.Trim());
                            }                            
                            acc.upd_table(s_mmyy, s_userid);
                            MessageBox.Show(lan.Change_language_MessageText("Đã tạo xong!"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); 
                        }
                        else
                            return;
                    }
                    else
                    {
                        lbl.Text =lan.Change_language_MessageText("Đang kiểm tra dữ liệu! Vui lòng đợi.");
                        lbl.Update();
                        this.Cursor = Cursors.Hand;
                        d1 = new DateTime(int.Parse(txtNam.Text.Trim()), int.Parse(txtThang.Text.Trim()), 1);
                        d2 = d1.AddMonths(1);
                        if (!acc.bVantay(s_mmyy))
                        {
                            kiemtra_dlieu_chamcong(s_mmyy);
                            kiemtra_dlieu_chamcong_tt(s_mmyy);
                            if (acc.cc_tructc(s_mmyy).ToString().Trim() == "1")
                            {
                                kiemtra_dlieu_chamcong_tructc(s_mmyy);
                            }
                        }
                        kiemtra_dlieu_thchamcong(s_mmyy);
                        kiemtra_dlieu_hesonv(s_mmyy, txtNam.Text.Trim(), txtThang.Text.Trim());
                        kiemtra_dlieu_phepnam(s_mmyy, txtNam.Text.Trim());
                        acc.kt_ngaynghiphep(s_mmyy, txtNam.Text.Trim());
                        acc.kt_ngaynghibu(s_mmyy, txtNam.Text.Trim());
                        lbl.Text = "Xong.";
                        lbl.Update();
                        this.Cursor = Cursors.Default;
                    }
                    break;
            }
		}
        private void tao_dlheso(string mmyy, string mmyy_truoc)
        {
            //cap nhat bang he so
            string sql = " insert into " + user + ".heso ";
            sql += " (mmyy,gbd_ccchieu,cccuoituan,cc_ngaycn,ccthuongquy,cctructc,khoadl,nhapmacc,thcctudong,sd_ccngayt7,sd_tgsomtre,cctc_taikhoa,tn_thcc,dn_thcc,vantay,mltt,ngachdn,hsdst_abc,stpc_abc,h_abc,";
            sql += " h_phepthem,h_tt,h_ng,h_tc,h_tq,khoadl_phep,pc_tienan,pc_tacatruc,lg_luutruhs,luonghd,hd_bantg,luong_sp,h_hsvk,h_hscv,h_hsdb,h_hsdh,h_hsdt,h_hskv,h_hsld,h_hsth,h_hstn,h_hsud,";
            sql += " h_hskh,h_hsta,h_hstx,h_hstdt,mtietkiem_hs,mltt_phucap,lamviec,snlvthucte,mbhxh_hs,mbhyt_hs,mbhtn_hs,sbhxh_hs,sbhyt_hs,";
            sql += " sbhtn_hs,gbdng_n,gbdng_d,gktng_n,gktng_d, mcaba_hs,mcn_hs,mletet_hs,pcngnt_d,pcngcn_d,pcnglt_d,hs1,hs2,hs3,hs4,ma,mb,mc,thuonga,";
            sql += " thuongb,thuongc,pctt,pctt1,pctt2,hpctt,hpctt1,hpctt2,mom_hs,lg_tapsu,lg_dihoc,mkpcd_hs,lanccvt,pctq_nt,";
            sql += " pctq_cn,pctq_lt,pctc_nt,pctc_cn,pctc_lt,hpctc_nt,hpctc_cn,hpctc_lt)";
            sql += " select '" + mmyy + "' as mmyy,gbd_ccchieu,cccuoituan,cc_ngaycn,ccthuongquy,cctructc,khoadl,nhapmacc,thcctudong,sd_ccngayt7,sd_tgsomtre,cctc_taikhoa,tn_thcc,dn_thcc,vantay,mltt,ngachdn,hsdst_abc,stpc_abc,h_abc,";
            sql += " h_phepthem,h_tt,h_ng,h_tc,h_tq,khoadl_phep,pc_tienan,pc_tacatruc,lg_luutruhs,luonghd,hd_bantg,luong_sp,h_hsvk,h_hscv,h_hsdb,h_hsdh,h_hsdt,h_hskv,h_hsld,h_hsth,h_hstn,h_hsud,";
            sql += " h_hskh,h_hsta,h_hstx,h_hstdt,mtietkiem_hs,mltt_phucap,lamviec,snlvthucte,mbhxh_hs,mbhyt_hs,mbhtn_hs,sbhxh_hs,sbhyt_hs,";
            sql += " sbhtn_hs,gbdng_n,gbdng_d,gktng_n,gktng_d, mcaba_hs,mcn_hs,mletet_hs,pcngnt_d,pcngcn_d,pcnglt_d,hs1,hs2,hs3,hs4,ma,mb,mc,thuonga,";
            sql += " thuongb,thuongc,pctt,pctt1,pctt2,hpctt,hpctt1,hpctt2,mom_hs,lg_tapsu,lg_dihoc,mkpcd_hs,lanccvt,pctq_nt,";
            sql += " pctq_cn,pctq_lt,pctc_nt,pctc_cn,pctc_lt,hpctc_nt,hpctc_cn,hpctc_lt";
            sql += "  from "+user+".heso where mmyy='"+mmyy_truoc+"'";
            acc.Execute_data(sql);
            //cap nhat khoa du lieu thang truoc
            sql = " update "+user+".heso set khoadl=1 where mmyy='"+mmyy_truoc.Trim()+"' ";            
            acc.Execute_data(sql);
            //cap nhat he so bao hiem
            sql = " insert into "+user+".heso_bh ";
            sql += " select loaibh,loaihd,hs_cv,hs_vk,hs_kv,hs_tn,hs_dh,luongkhoan,";
            sql += " hs_lc,'"+mmyy.Trim()+"' as mmyy from "+user+".heso_bh where mmyy='"+mmyy_truoc.Trim()+"'";
            acc.Execute_data(sql);
            //cap nhat phu cap boi duong pttt
            sql = " insert into "+user+".boiduong_pttt";
            sql += " select pttt,loai,tenloai,ptv,phu1,phu2,bsgayme,ktvgayme,hoisuc,dungcu,'"+s_mmyy.Trim()+"' as mmyy";
            sql += " ,giupviec from "+user+".boiduong_pttt where mmyy='"+mmyy_truoc.Trim()+"'";
            acc.Execute_data(sql); 
            //cap nhat bang tyle_ttncn
            decimal id=0;
            sql = " select max(id) as id from "+user+".tyle_ttncn ";
            ds = acc.get_data(sql);
            if(ds.Tables[0].Rows.Count>0)
            {
                id= decimal.Parse(ds.Tables[0].Rows[0]["id"].ToString().Trim());
            }
            sql = " select * from "+user+".tyle_ttncn where mmyy='"+mmyy_truoc.Trim()+"' ";
            ds=acc.get_data(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (acc.get_data("select mmyy from " + user + ".tyle_ttncn where mmyy='" + mmyy.Trim() + "'").Tables[0].Rows.Count == 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        id += 1;
                        sql = " insert into " + user + ".tyle_ttncn(id,gh_duoi,gh_tren,tyle_thue,mmyy) ";
                        sql += " values(" + id + "," + dr["gh_duoi"].ToString() + "," + dr["gh_tren"].ToString() + "," + dr["tyle_thue"].ToString() + ",'" + mmyy.Trim() + "')";
                        acc.Execute_data(sql);
                    }
                }
            }
        }
        private void upd_phepcon(string s_yyyy)
        {
            DataSet ds = new DataSet();
            int nam_t = int.Parse(s_yyyy) - 1;
            string sql = "";
            sql = " select manv,(phep+phepthem)-(t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12) as ts_phepcon";
            sql += " from " + acc.user + ".phepnam_"+nam_t+" where nam='" + nam_t.ToString().Trim() + "'";
            if (acc.byyyy(nam_t.ToString().Trim()))
            {
                ds = acc.get_data(sql);
            }
            else
            {
                ds = null;
            }
            if (ds!=null)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    sql = "update "+acc.user+".phepnam_"+s_yyyy.Trim()+" set ts_ngayton="+dr["ts_phepcon"].ToString()+"";
                    sql += " where manv='"+dr["manv"].ToString().Trim()+"' and nam='"+s_mmyy.Trim()+"'";
                    acc.Execute_data(sql);
                }
            }
        }
        private void ktthang_khoaphep(string s_mmyy,string s_mm,string s_yyyy)
        {
            int mm = s_mm.Trim() != "" ? int.Parse(s_mm.Trim()) : 1;
            if (acc.khoadl_phep(s_mmyy) == 1)
            {
                mm = 2;
            }
            decimal ts_ngayton = 0, tsp_danghi = 0;
            string s_tmp = "";
            string sql = " select p.manv,p.madv,p.ts_ngayton,sum(";
            for (int i = 1; i < mm; i++)
            {
                s_tmp= " t"+i.ToString().Trim()+" +";
            }
            s_tmp = s_tmp.Length >0 ? s_tmp.Trim().Substring(0,s_tmp.Length-1):"";
            sql += s_tmp;
            sql+=") as tsp_danghi from "+acc.user+".phepnam_"+s_yyyy.Trim()+" order by manv";
            DataSet ds = acc.get_data(sql);
            if (ds!=null)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ts_ngayton = dr["ts_ngayton"].ToString().Trim() != "" ? decimal.Parse(dr["ts_ngayton"].ToString().Trim()) : 0;
                    tsp_danghi = dr["tsp_danghi"].ToString().Trim() != "" ? decimal.Parse(dr["tsp_danghi"].ToString().Trim()) : 0;
                    if (ts_ngayton > tsp_danghi)
                    {
                        ts_ngayton = tsp_danghi;
                    }
                    if (ts_ngayton == tsp_danghi)
                    {
                        ts_ngayton = 0;
                    }
                    sql = " update "+acc.user+".phepnam_"+s_yyyy+" set ts_ngayton="+ts_ngayton.ToString().Trim()+" ";
                    sql += " where manv='"+dr["manv"].ToString().Trim()+"' and madv='"+dr["madv"].ToString().Trim()+"' and nam='"+s_yyyy.Trim()+"'";
                    acc.Execute_data(sql);
                }
            }             
        }
        private bool kiemtra_dlieu_chamcong(string s_mmyy)
        {
            DataSet ds = new DataSet();
            DataSet ds1 = new DataSet();
            string sql = "";
            sql = " select l.manv,l.madv from " + acc.user + ".llnv l inner join " + acc.user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+acc.user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and to_timestamp(to_char(hd.ngaydenct,'yyyy/mm/dd'),'yyyy/mm/dd') < to_timestamp('" + d2.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd')";
            sql += " and (hd.ngaynghi is null or to_timestamp(to_char(hd.ngaynghi,'yyyy/mm/dd'),'yyyy/mm/dd') > to_timestamp('"+d1.ToString("yyyy/MM/dd",null)+"','yyyy/MM/dd'))";
            sql += " order by l.manv";
            ds = acc.get_data(sql);
            ds1 = acc.get_data(" select * from " + acc.user + ".chamcong_"+s_mmyy+" where mmyy='" + s_mmyy + "'");
            DataRow row;
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds1.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = "insert into " + acc.user + ".chamcong_"+s_mmyy+" (manv,madv,mmyy) values ";
                    sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_mmyy + "')";
                    try
                    {
                        acc.Execute_data(sql);                        
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            // delete
            foreach (DataRow r in ds1.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = " delete from  " + acc.user + ".chamcong_"+s_mmyy+" where manv='" + r["manv"].ToString()+"'";
                    sql += " and mmyy='" + s_mmyy + "'";
                    try
                    {
                        acc.Execute_data(sql);                        
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        private bool kiemtra_dlieu_chamcong_tt(string s_mmyy)
        {
            //bool gttv = false;
            DataSet ds = new DataSet();
            DataSet ds1 = new DataSet();
            string sql = "";
            sql = " select l.manv,l.madv from " + acc.user + ".llnv l inner join " + acc.user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + acc.user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and to_timestamp(to_char(hd.ngaydenct,'yyyy/mm/dd'),'yyyy/mm/dd') < to_timestamp('" + d2.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd')";
            sql += " and (hd.ngaynghi is null or to_timestamp(to_char(hd.ngaynghi,'yyyy/mm/dd'),'yyyy/mm/dd') > to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'))";
            sql += " order by l.manv";
            ds = acc.get_data(sql);
            ds1 = acc.get_data("Select  * from " + acc.user + ".chamcong_thuongtruc_"+s_mmyy+" where mmyy='" + s_mmyy + "'");
            DataRow row;
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds1.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = "insert into " + acc.user + ".chamcong_thuongtruc_"+s_mmyy+" (manv,madv,mmyy) values ";
                    sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_mmyy + "')";
                    try
                    {
                        acc.Execute_data(sql);                        
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            // delete
            foreach (DataRow r in ds1.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = " delete from  " + acc.user + ".chamcong_thuongtruc_"+s_mmyy+" where manv='" + r["manv"].ToString() + "'";
                    sql += " and mmyy='" + s_mmyy + "'";
                    try
                    {
                        acc.Execute_data(sql);                        
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        
        private bool kiemtra_dlieu_chamcong_tructc(string s_mmyy)
        {
            //bool gttv = false;
            DataSet ds = new DataSet();
            DataSet ds1 = new DataSet();
            string sql = "";
            sql = " select l.manv,l.madv from " + acc.user + ".llnv l inner join " + acc.user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + acc.user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and to_timestamp(to_char(hd.ngaydenct,'yyyy/mm/dd'),'yyyy/mm/dd') < to_timestamp('" + d2.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd')";
            sql += " and (hd.ngaynghi is null or to_timestamp(to_char(hd.ngaynghi,'yyyy/mm/dd'),'yyyy/mm/dd') > to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'))";
            sql += " order by l.manv";
            ds = acc.get_data(sql);
            ds1 = acc.get_data("select * from " + acc.user + ".chamcong_tructc_"+s_mmyy+" where mmyy='" + s_mmyy + "'");
            DataRow row;
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds1.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = "insert into " + acc.user + ".chamcong_tructc_"+s_mmyy+" (manv,madv,mmyy) values ";
                    sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_mmyy + "')";
                    try
                    {
                        acc.Execute_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            // delete
            foreach (DataRow r in ds1.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = "delete from  " + acc.user + ".chamcong_tructc_"+s_mmyy+" where manv='" + r["manv"].ToString() + "'";
                    sql += " and mmyy='" + s_mmyy + "'";
                    try
                    {
                        acc.Execute_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        //kiem tra du lieu bang tong hop cham cong
        private bool kiemtra_dlieu_thchamcong(string s_mmyy)
        {
            DataSet ds = new DataSet();
            DataSet ds1 = new DataSet();
            string sql = "";
            sql = " select l.manv,l.madv from " + acc.user + ".llnv l inner join " + acc.user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join " + acc.user + ".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and to_timestamp(to_char(hd.ngaydenct,'yyyy/mm/dd'),'yyyy/mm/dd') < to_timestamp('" + d2.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd')";
            sql += " and (hd.ngaynghi is null or to_timestamp(to_char(hd.ngaynghi,'yyyy/mm/dd'),'yyyy/mm/dd') > to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'))";
            sql += " order by l.manv";
            ds = acc.get_data(sql);
            ds1 = acc.get_data(" select * from " + acc.user + ".th_chamcong_" + s_mmyy + " where mmyy='" + s_mmyy + "'");
            DataRow row;
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds1.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = "insert into " + acc.user + ".th_chamcong_" + s_mmyy + " (manv,madv,mmyy) values ";
                    sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_mmyy + "')";
                    try
                    {
                        acc.Execute_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            // delete
            foreach (DataRow r in ds1.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = " delete from  " + acc.user + ".th_chamcong_" + s_mmyy + " where manv='" + r["manv"].ToString() + "'";
                    sql += " and mmyy='" + s_mmyy + "'";
                    try
                    {
                        acc.Execute_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        private bool kiemtra_dlieu_hesonv(string s_mmyy,string yyyy,string mm)
        {
            DataSet ds = new DataSet();
            DataSet ds1 = new DataSet();
            DateTime d1, d2;
            d1 = new DateTime(int.Parse(yyyy), int.Parse(mm), 1);
            d2 = d1.AddMonths(1);
            string sql = "";
            sql = " select l.manv,l.madv,'" + s_mmyy + "',h.bhxh,h.bhyt,h.bhtn";
            sql += " ,case when (h.cdoanphi is not null and to_timestamp(to_char(h.cdoanphi,'yyyy/mm/dd'),'yyyy/mm/dd') <= to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/mm/dd')) then 1 else 0 end as kpcd";
            sql += " ,case when h.heso<>'' then h.heso else '0' end as heso,h.hs_lc,h.hs_cv,h.hs_vk,h.hs_tn,h.hs_kv";
            sql += " ,h.hs_dh,h.hs_db,h.hs_dt,h.hs_ld,h.hs_th,h.hs_ud,h.pc_ta,h.pc_tx,h.pc_tdt,h.pc_khac,h.sotien_hd,h.lhd_bantg,h.st_bantg,h.luong_sp,h.tongluong";
            sql += " ,h.loaihd,case when h.loaihd='TV' then 1 else 0 end as thuviec,case when h.magc ='02' then 1 else 0 end as dihoc,case when h.magc ='01' then 1 else 0 end as luutruhs ";
            sql += " from " + acc.user + ".hopdong h inner join " + acc.user + ".llnv l on h.manv=l.manv ";
            sql += " inner join " + acc.user + ".max_lan ml on h.manv=ml.manv";
            sql += " left join " + acc.user + ".mahd mh on h.loaihd=mh.mahd";
            sql += " where h.lan =ml.lan  ";
            sql += " and (to_timestamp(to_char(h.ngaydenct,'yyyy/mm/dd'),'yyyy/mm/dd') < to_timestamp('" + d2.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'))";
            sql += " and (h.ngaynghi is null or to_timestamp(to_char(h.ngaynghi,'yyyy/mm/dd'),'yyyy/mm/dd') > to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'))";
            sql += " order by l.manv";
            ds = acc.get_data(sql);
            ds1 = acc.get_data(" select * from " + acc.user + ".heso_nv_" + s_mmyy + " where mmyy='" + s_mmyy + "'");
            DataRow row;
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds1.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = "insert into " + acc.user + ".heso_nv_" + s_mmyy + "";
                    sql += "(manv,madv,mmyy,bhxh,bhyt,bhtn,kpcd,h_hsbdhv,h_hslc,h_hscv,h_hsvk,h_hstn,h_hskv,h_hsdh,h_hsdb,h_hsdt,h_hsld,";
                    sql += " h_hsth,h_hsud,h_pcta,h_pctx,h_pctdt,h_pckh,h_sotienhd,h_lhdbantg,h_stbantg,h_luongsp,h_tongluong,loaihd ,thuviec,dihoc,luutruhs)";
                    sql += " values('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','"+s_mmyy+"'," + r["bhxh"].ToString() + ",";
                    sql += " " + r["bhyt"].ToString() + "," + r["bhtn"].ToString() + "," + r["kpcd"].ToString() + ",";
                    sql += " '" + r["heso"].ToString() + "'," + r["hs_lc"].ToString() + "," + r["hs_cv"].ToString() + ",";
                    sql += " " + r["hs_vk"].ToString() + "," + r["hs_tn"].ToString() + "," + r["hs_kv"].ToString() + ",";
                    sql += " " + r["hs_dh"].ToString() + "," + r["hs_db"].ToString() + "," + r["hs_dt"].ToString() + "," + r["hs_ld"].ToString() + ",";
                    sql += " " + r["hs_th"].ToString() + "," + r["hs_ud"].ToString() + "," + r["pc_ta"].ToString() + ",";
                    sql += " " + r["pc_tx"].ToString() + "," + r["pc_tdt"].ToString() + "," + r["pc_khac"].ToString() + "," + r["sotien_hd"].ToString() + ",";
                    sql += r["lhd_bantg"].ToString().Trim()!="" ? " " + r["lhd_bantg"].ToString() + ",": "'',";
                    sql += " " + r["st_bantg"].ToString() + "," + r["luong_sp"].ToString() + "," + r["tongluong"].ToString() + ",";
                    sql += " '" + r["loaihd"].ToString() + "'," + r["thuviec"].ToString() + "," + r["dihoc"].ToString() + "," + r["luutruhs"].ToString() + ")";
                    try
                    {
                        acc.Execute_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
                sql = "update " + acc.user + ".heso_nv_" + s_mmyy + " set bhxh=" + r["bhxh"].ToString() + "";
                sql += " ,bhyt=" + r["bhyt"].ToString() + ",bhtn=" + r["bhtn"].ToString() + ",kpcd=" + r["kpcd"].ToString() + "";
                sql += " ,h_hsbdhv='" + r["heso"].ToString() + "',h_hslc=" + r["hs_lc"].ToString() + ",h_hscv=" + r["hs_cv"].ToString() + "";
                sql += " ,h_hsvk=" + r["hs_vk"].ToString() + ",h_hstn=" + r["hs_tn"].ToString() + ",h_hskv=" + r["hs_kv"].ToString() + "";
                sql += " ,h_hsdh=" + r["hs_dh"].ToString() + ",h_hsdb=" + r["hs_db"].ToString() + ",h_hsdt=" + r["hs_dt"].ToString() + ",h_hsld=" + r["hs_ld"].ToString() + "";
                sql += " ,h_hsth=" + r["hs_th"].ToString() + ",h_hsud=" + r["hs_ud"].ToString() + ",h_pcta=" + r["pc_ta"].ToString() + "";
                sql += " ,h_pctx=" + r["pc_tx"].ToString() + ",h_pctdt=" + r["pc_tdt"].ToString() + ",h_pckh=" + r["pc_khac"].ToString() + ",h_sotienhd=" + r["sotien_hd"].ToString() + "";
                sql +=   r["lhd_bantg"].ToString().Trim() != "" ? " ,h_lhdbantg='" + r["lhd_bantg"].ToString() + "'" : ",h_lhdbantg='' ";
                sql += " ,h_stbantg=" + r["st_bantg"].ToString() + ",h_luongsp=" + r["luong_sp"].ToString() + "";
                sql += " ,h_tongluong=" + r["tongluong"].ToString() + "";
                sql += " ,loaihd='" + r["loaihd"].ToString() + "',thuviec=" + r["thuviec"].ToString() + ",dihoc=" + r["dihoc"].ToString() + ",luutruhs=" + r["luutruhs"].ToString() + " ";
                sql += " where manv='" + r["manv"].ToString() + "'and madv='" + r["madv"].ToString() + "' and mmyy='" + s_mmyy + "'";
                try
                {
                    acc.Execute_data(sql);
                }
                catch
                {
                    return false;
                }
            }
            // delete
            foreach (DataRow r in ds1.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = " delete from  " + acc.user + ".heso_nv_" + s_mmyy + " where manv='" + r["manv"].ToString() + "'";
                    sql += " and mmyy='" + s_mmyy + "' and madv='"+r["madv"].ToString().Trim()+"'";
                    try
                    {
                        acc.Execute_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        private bool kiemtra_dlieu_phepnam(string s_mmyy,string s_yyyy)
        {
            string sql1 = "";
            DataSet ds = new DataSet();
            DataSet ds1 = new DataSet();
            string sql = "",phepthem="";
            phepthem = acc.phepthem(s_mmyy).Trim();
            sql = " select l.manv,l.madv,hd.sonphep,hd.lan ";
            if(phepthem.Substring(0,1).Trim()=="1")
                sql += ",floor(extract(year from age(to_timestamp('"+d1.ToString("yyyy/MM/dd",null)+"','yyyy/MM/dd'),to_timestamp(to_char(hd.ngaybhxh,'yyyy/mm/dd'),'yyyy/mm/dd')))/5) as phepthem";
            if (phepthem.Substring(1, 1).Trim() == "1")
            {
                sql += ",case when extract(year from age(to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'),to_timestamp(to_char(hd.ngaybhxh,'yyyy/mm/dd'),'yyyy/mm/dd'))) between 5 and 10 then 1 else ";
                sql += "floor(extract(year from age(to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'),to_timestamp(to_char(hd.ngaybhxh,'yyyy/mm/dd'),'yyyy/mm/dd')))/5) end as phepthem";
            }
            sql += " from " + acc.user + ".llnv l inner join " + acc.user + ".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+acc.user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and to_timestamp(to_char(hd.ngaydenct,'yyyy/mm/dd'),'yyyy/mm/dd') < to_timestamp('" + d2.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd')";
            sql += " and (hd.ngaynghi is null or to_timestamp(to_char(hd.ngaynghi,'yyyy/mm/dd'),'yyyy/mm/dd') > to_timestamp('" + d1.ToString("yyyy/MM/dd", null) + "','yyyy/MM/dd'))";
            sql += " order by l.manv";
            ds = acc.get_data(sql);
            ds1 = acc.get_data("Select  * from " + acc.user + ".phepnam_"+s_yyyy+" where nam='" + s_yyyy + "'");
            DataRow row;
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                row = acc.getrowbyid(ds1.Tables[0], "manv='" + r["manv"].ToString() + "'");
                if (row == null)
                {
                    sql = "insert into " + acc.user + ".phepnam_"+s_yyyy+" (manv,madv,nam,phep,phepthem) values ";
                    sql += " ('" + r["manv"].ToString() + "','" + r["madv"].ToString() + "','" + s_yyyy + "',";
                    sql += " " + r["sonphep"].ToString() + ",";
                    sql += r["phepthem"].ToString().Trim()!="" ?  " " + r["phepthem"].ToString() + "":"0";
                    sql += ")";
                    try
                    {
                        acc.Execute_data(sql);
                    }
                    catch
                    {
                        return false;
                    }
                }
                else
                {
                    sql = "update " + acc.user + ".phepnam_"+s_yyyy+"  set phep="+r["sonphep"].ToString()+"";
                    sql += r["phepthem"].ToString().Trim()!="" ? ",phepthem="+r["phepthem"].ToString() +"": ",phepthem=0";
                    sql += "  where manv='"+r["manv"].ToString()+"' and nam='"+ s_yyyy + "'";
                    //cap nhat ngay phep them hang nam cho bang hop dong
                    sql1 = " update " + acc.user + ".hopdong set  ";
                    sql1 +=r["phepthem"].ToString().Trim()!="" ? " phepthem=" + r["phepthem"].ToString() + "":"phepthem=0";
                    sql1 += " where manv='" + r["manv"].ToString() + "' and lan=" + r["lan"].ToString() + "";
                    try
                    {
                        acc.Execute_data(sql);
                        acc.Execute_data(sql1);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }            
            return true;
        }

		private void txtNam_Validated(object sender, System.EventArgs e)
		{
			try
			{
				if ((int.Parse(txtNam.Text)<DateTime.Now.Year-4)||(int.Parse(txtNam.Text)>DateTime.Now.Year+1))
				{
					MessageBox.Show(lan.Change_language_MessageText("Năm không hợp lệ!"),this.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning);
					txtNam.Focus();
					return ;
				}
			}
			catch
			{
				txtNam.Text=DateTime.Now.Year.ToString();
			}
		}

		private void txtThang_Validated(object sender, System.EventArgs e)
		{
			txtThang.Text=txtThang.Text.PadLeft(2,'0');
			try
			{
				if ((int.Parse(txtThang.Text)<1)||(int.Parse(txtThang.Text)>12))
				{
					MessageBox.Show(lan.Change_language_MessageText("Tháng không hợp lệ!"),AccessData.Msg,MessageBoxButtons.OK,MessageBoxIcon.Warning);
					txtThang.Focus();
					return ;
				}
			}
			catch
			{
				txtThang.Text=DateTime.Now.Month.ToString().PadLeft(2,'0');
			}
		}
        private void txtThang_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txtNam_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
        private void rdbKiemtra_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbKiemtra.Checked)
                gCheck_new.Tag = "1";
            else
                gCheck_new.Tag = "";
        }

        private void rdbNew_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbNew.Checked)
                gCheck_new.Tag = "2";
            else
                gCheck_new.Tag = "";
        }		
	}
}
